package edu.fjnu.datagenerator.engine;




import edu.fjnu.datagenerator.campusTraffic.CampusTrafficEntity;
import edu.fjnu.datagenerator.campusTraffic.DateScope;
import org.apache.commons.scxml2.Context;
import org.apache.commons.scxml2.Evaluator;
import org.apache.commons.scxml2.SCXMLExecutor;
import org.apache.commons.scxml2.TriggerEvent;
import org.apache.commons.scxml2.env.SimpleErrorReporter;
import org.apache.commons.scxml2.env.jexl.JexlEvaluator;
import org.apache.commons.scxml2.io.SCXMLReader;;
import org.apache.commons.scxml2.model.SCXML;
import org.apache.commons.scxml2.model.ModelException;

import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hadoop on 16-3-25.
 */
public class SCXMLEngine {


    private SCXMLExecutor executor = null;

    public void initEngine() {
        //得到资源文件路径
        final URL datamachine = this.getClass().getClassLoader().getResource("datamachine.xml");
        //实例化数据模型解析器
        Evaluator evaluator = new JexlEvaluator();

        //实例化引擎
        executor = new SCXMLExecutor(evaluator, null, new SimpleErrorReporter());

        try {
            //加载资源文件,实例化到一个SCXML对象，两者之间一一对应
            SCXML scxml = SCXMLReader.read(datamachine);

            //将这样的一个SCXML实例，作为状态机对象，传入到引擎里面。
            executor.setStateMachine(scxml);

            //设置引擎执行的根上下文
            Context rootContext = evaluator.newContext(null);
            CampusTrafficEntity campusTrafficEntity = new CampusTrafficEntity();
            rootContext.set("campusTrafficEntity", campusTrafficEntity);
            executor.setRootContext(rootContext);

            //开始启动流程
            executor.go();

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(executor.getGlobalContext().getSystemContext().get("_sessionid"));


    }

    public void startEngine() {

        try {
            CampusTrafficEntity campusTrafficEntity = new CampusTrafficEntity();
            DateScope dateScope = new DateScope();
            //日期范围
            dateScope.setStartDate("2016-03-01 00:00:00");
            dateScope.setStopDate("2016-03-07 00:00:00");
            //起始日期
            String date = dateScope.getStartDate();
            int carId = 0;
            String licensePlate;
            String identity;
            //将日期由String转换为Date类型
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date_stop = sdf.parse(dateScope.getStopDate());
            Date date_now = sdf.parse(dateScope.getStartDate());
            //当时间超过结束日期，引擎停止运行
            while (date_stop.getTime() > date_now.getTime()) {

                //设置日期
                Map<String, String> setDate = new HashMap<String, String>();
                setDate.put("startDate", dateScope.getStartDate());
                setDate.put("stopDate", dateScope.getStopDate());
                //携带外部数据进去，然后触发
                executor.triggerEvent(new TriggerEvent("setDate.end", TriggerEvent.SIGNAL_EVENT, setDate));
                //生成车辆信息
                carId = Integer.parseInt(campusTrafficEntity.getCarIdByASC(carId));
                date = campusTrafficEntity.getDateByRandom(date);
                licensePlate = campusTrafficEntity.getLicensePlateByRandom();
                identity = campusTrafficEntity.getIdentityByRandom();
                Map<String, String> carData = new HashMap<String, String>();
                carData.put("carId", String.valueOf(carId));
                carData.put("date", date);
                carData.put("licensePlate", licensePlate);
                carData.put("identity", identity);
                carData.put("log", "车辆信息：" + carId + "|" + date + "|" + licensePlate + "|" + identity);
                //携带外部数据进去，然后触发
                executor.triggerEvent(new TriggerEvent("getCarInfo.end", TriggerEvent.SIGNAL_EVENT, carData));
                //将日期由String转换为Date类型
                date_now = sdf.parse(date);

            }
        } catch (ModelException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
